package com.loan.market.ui

import android.content.Intent
import android.os.Bundle
import androidx.core.widget.addTextChangedListener
import com.loan.market.R
import com.loan.market.base.BaseBindingActivity
import com.loan.market.databinding.ActivityEditNameBinding

class EditNameActivity : BaseBindingActivity<ActivityEditNameBinding>() {

    override fun getViewBinding(): ActivityEditNameBinding {
        return ActivityEditNameBinding.inflate(layoutInflater)
    }

    override fun initUi(savedInstanceState: Bundle?) {
    }

    private val chars = arrayListOf("@", "<", ">", "/")

    override fun initEvent() {
        binding.etName.addTextChangedListener {
            val name = binding.etName.text
            if (name.length >= 2 && chars.none { name.contains(it) }) {
                binding.tvTip.setTextColor(getColor(R.color.color_999))
            } else {
                binding.tvTip.setTextColor(getColor(R.color.color_red))
            }
        }
        binding.btnSave.setOnClickListener {
            val name = binding.etName.text
            if (name.length < 2 || chars.any { name.contains(it) }) return@setOnClickListener
            val intent = Intent()
            intent.putExtra("name", name.toString())
            setResult(RESULT_OK, intent)
            finish()
        }
    }

    override fun initData() {
    }
}